using System;
using System.Drawing;
using System.Windows.Forms;
using System.Timers;

namespace ScreenKeyboardTester
{
    public class ScreenTestPanel : Panel
    {
        private ComboBox? colorComboBox;
        private Button? startButton;
        private Button? stopButton;
        private Panel? testArea;
        private System.Timers.Timer? colorTimer;
        private int currentColorIndex = 0;
        private Color[]? testColors;
        private bool isTesting = false;

        public ScreenTestPanel()
        {
            InitializeComponent();
            SetupTestColors();
        }

        private void InitializeComponent()
        {
            this.BackColor = SystemColors.Control;
            this.Dock = DockStyle.Fill; // 使面板填充整个选项卡

            // 颜色选择下拉框
            colorComboBox = new ComboBox
            {
                Location = new Point(20, 20),
                Size = new Size(150, 25),
                DropDownStyle = ComboBoxStyle.DropDownList,
                Anchor = AnchorStyles.Top | AnchorStyles.Left
            };
            colorComboBox.Items.AddRange(new object[] {
                "纯色测试",
                "颜色循环",
                "灰度渐变",
                "RGB测试"
            });
            colorComboBox.SelectedIndex = 0;

            // 开始按钮
            startButton = new Button
            {
                Location = new Point(180, 20),
                Size = new Size(80, 25),
                Text = "开始测试",
                Anchor = AnchorStyles.Top | AnchorStyles.Left
            };
            startButton.Click += StartButton_Click;

            // 停止按钮
            stopButton = new Button
            {
                Location = new Point(270, 20),
                Size = new Size(80, 25),
                Text = "停止测试",
                Enabled = false,
                Anchor = AnchorStyles.Top | AnchorStyles.Left
            };
            stopButton.Click += StopButton_Click;

            // 测试区域
            testArea = new Panel
            {
                Location = new Point(20, 60),
                Size = new Size(760, 500),
                BackColor = Color.White,
                BorderStyle = BorderStyle.FixedSingle,
                Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right // 自适应大小
            };

            // 添加控件
            this.Controls.Add(colorComboBox);
            this.Controls.Add(startButton);
            this.Controls.Add(stopButton);
            this.Controls.Add(testArea);

            // 设置定时器
            colorTimer = new System.Timers.Timer(1000); // 1秒间隔
            colorTimer.Elapsed += ColorTimer_Elapsed;
        }

        private void SetupTestColors()
        {
            testColors = new Color[]
            {
                Color.Black,
                Color.White,
                Color.Red,
                Color.Green,
                Color.Blue,
                Color.Yellow,
                Color.Magenta,
                Color.Cyan,
                Color.Gray
            };
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            isTesting = true;
            if (startButton != null) startButton.Enabled = false;
            if (stopButton != null) stopButton.Enabled = true;
            if (colorComboBox != null) colorComboBox.Enabled = false;

            if (colorComboBox != null && testArea != null)
            {
                switch (colorComboBox.SelectedIndex)
                {
                    case 0: // 纯色测试
                        testArea.BackColor = Color.Red;
                        break;
                    case 1: // 颜色循环
                        if (colorTimer != null) colorTimer.Start();
                        break;
                    case 2: // 灰度渐变
                        StartGradientTest();
                        break;
                    case 3: // RGB测试
                        StartRGBTest();
                        break;
                }
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            StopTest();
        }

        private void ColorTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (isTesting && colorComboBox != null && colorComboBox.SelectedIndex == 1) // 颜色循环
            {
                this.Invoke(new Action(() =>
                {
                    if (testArea != null && testColors != null)
                    {
                        testArea.BackColor = testColors[currentColorIndex];
                        currentColorIndex = (currentColorIndex + 1) % testColors.Length;
                    }
                }));
            }
        }

        private void StartGradientTest()
        {
            var gradientTimer = new System.Timers.Timer(100);
            int gradientValue = 0;
            bool increasing = true;

            gradientTimer.Elapsed += (s, e) =>
            {
                if (!isTesting) return;

                this.Invoke(new Action(() =>
                {
                    testArea.BackColor = Color.FromArgb(gradientValue, gradientValue, gradientValue);
                    
                    if (increasing)
                    {
                        gradientValue += 5;
                        if (gradientValue >= 255)
                        {
                            gradientValue = 255;
                            increasing = false;
                        }
                    }
                    else
                    {
                        gradientValue -= 5;
                        if (gradientValue <= 0)
                        {
                            gradientValue = 0;
                            increasing = true;
                        }
                    }
                }));
            };

            gradientTimer.Start();
        }

        private void StartRGBTest()
        {
            var rgbTimer = new System.Timers.Timer(500);
            int rgbIndex = 0;

            rgbTimer.Elapsed += (s, e) =>
            {
                if (!isTesting) return;

                this.Invoke(new Action(() =>
                {
                    switch (rgbIndex)
                    {
                        case 0:
                            testArea.BackColor = Color.Red;
                            break;
                        case 1:
                            testArea.BackColor = Color.Green;
                            break;
                        case 2:
                            testArea.BackColor = Color.Blue;
                            break;
                    }
                    
                    rgbIndex = (rgbIndex + 1) % 3;
                }));
            };

            rgbTimer.Start();
        }

        private void StopTest()
        {
            isTesting = false;
            if (colorTimer != null) colorTimer.Stop();
            if (startButton != null) startButton.Enabled = true;
            if (stopButton != null) stopButton.Enabled = false;
            if (colorComboBox != null) colorComboBox.Enabled = true;
            if (testArea != null) testArea.BackColor = Color.White;
        }

        public void Cleanup()
        {
            StopTest();
            colorTimer?.Dispose();
        }
    }
}